package com.nativetalk.controller.app.three.teacher.data;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.authentication.TdNtAuthenticationInsertTwo;
import com.nativetalk.bean.dict.TsNtDictDeatil;
import com.nativetalk.bean.dict.TsNtDictIndex;
import com.nativetalk.bean.dict.TsNtDictTea;
import com.nativetalk.bean.teacher.TdNtTeacherMinAmount;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.util.JsonUtil;
import com.nativetalk.common.util.MemcachedKey;
import com.nativetalk.common.util.MemcachedUtils;
import com.nativetalk.service.authentication.insert.AuthenticationInsertService;
import com.nativetalk.service.teacher.search.TeacherSearchService;
import com.nativetalk.service.teacher.update.TeacherUpdateService;
import com.nativetalk.service.three.teacher.update.ThreeTeacherUpdateService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by lizhun on 15/12/9.
 */
@Controller
public class ThreeTeacherdDataController {
    private static Logger logger = Logger.getLogger(ThreeTeacherdDataController.class);
    @Resource
    private ThreeTeacherUpdateService threeTeacherUpdateService;
    @Resource
    private AuthenticationInsertService authenticationInsertService;
    @Resource
    private TeacherSearchService teacherSearchService;
    @Resource
    private TeacherUpdateService teacherUpdateService;
    /**
     * @return RetInfo
     * @Purpose 更新用户自由话题价格
     * @version 3.0
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/teacher/minamount/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo hisList(@RequestBody TdNtTeacherMinAmount tdNtTeacherMinAmount,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtTeacherMinAmount));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            tdNtTeacherMinAmount.setTea_id(tdNtTeacherResult.getTea_id());
            retInfo = threeTeacherUpdateService.updateTeacherMinAmount(tdNtTeacherMinAmount,tdNtTeacherResult);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }


    }
    /**
     * @Purpose  申请认证
     * @version  3.0
     * @time     2015-01-11
     * @author   lizhun
     * @param     tdNtAuthenticationInsert
     * @return   RetInfo
     */
    @RequestMapping(value="/app/6/teatoken/teacher/apply" ,method = {RequestMethod.GET,RequestMethod.POST})
    @ResponseBody
    public RetInfo apply(@Valid @ModelAttribute TdNtAuthenticationInsertTwo tdNtAuthenticationInsert, BindingResult result,
                         @RequestHeader(value = "token", required = true) String token) throws IOException {
        RetInfo retInfo = new RetInfo();
        if (result.hasErrors())   {
            List<FieldError> err=result.getFieldErrors();
            FieldError    fe = err.get(0);
            retInfo.setMark(ValidationMessages.getValue("MARKPARAMETERERROR"));
            retInfo.setTip(fe.getDefaultMessage());
        }else {
            try {
                //打印入参
                logger.info(Constants.prefix_in + JsonUtil.toJson(tdNtAuthenticationInsert));

                TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
                tdNtAuthenticationInsert.setTea_id(tdNtTeacherResult.getTea_id());
                retInfo =  authenticationInsertService.insertAuthenticationTwo(tdNtAuthenticationInsert);

                //打印出参
                logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            } catch (Exception e) {
                //记录错误日志
                retInfo = RetInfo.getServerErrorInfo();
                logger.error(e.getMessage(), e);

                //打印出参
                logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
                return retInfo;
            }

        }
        return retInfo;
    }
    /**
     * @return RetInfo
     * @Purpose 修改支付方式
     * @version 3.0
     * @time    2015-05-14
     * @author lizhun
     */
    @RequestMapping(value = "/app/6/teatoken/teacher/pay/update", method = {RequestMethod.GET, RequestMethod.POST}, consumes = "application/json")
    @ResponseBody
    public RetInfo pay(@RequestBody TdNtTeacherResult params,
                           @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(params));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            tdNtTeacherResult.setPay_type(params.getPay_type());
            tdNtTeacherResult.setPayPal(params.getPayPal());
            retInfo = threeTeacherUpdateService.updateTeacherPay(tdNtTeacherResult);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @Purpose  获取个人资料
     * @version  1.0
     * @time     2015-01-09
     * @author   lizhun
     * @param    token
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/6/teatoken/teacher/data" ,method = {RequestMethod.GET,RequestMethod.POST} ,consumes = "application/json; charset=utf-8" )
    @ResponseBody
    public RetInfo data(@RequestHeader(value = "token", required = true) String token,
                        @RequestHeader(value = "version", required = true) int version) {
        RetInfo retInfo = new RetInfo();
        Map<String , Object> map = new HashMap<>();

        try {
            //打印入参
            logger.info(Constants.prefix_in);

            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            map.put("tea_sound_synopsis" , tdNtTeacherResult.getTea_sound_synopsis());
            map.put("tea_name" , tdNtTeacherResult.getTea_name());
            map.put("tea_email" , tdNtTeacherResult.getTea_email());
            map.put("tea_sex" , tdNtTeacherResult.getTea_sex());
            map.put("tea_birthday" , tdNtTeacherResult.getTea_birthday());
            map.put("tea_synopsis" , tdNtTeacherResult.getTea_synopsis());
            map.put("tea_head_portrait" , tdNtTeacherResult.getTea_head_portrait());
            map.put("payPal" , tdNtTeacherResult.getPayPal());
            map.put("contact",tdNtTeacherResult.getContact());
            map.put("min_amount" , tdNtTeacherResult.getMin_amount());
            TdNtTeacherResult teacher = teacherSearchService.findByTeaId(tdNtTeacherResult.getTea_id());
            map.put("tea_intro_video_cover", teacher.getTea_intro_video_cover());
            map.put("tea_intro_video", teacher.getTea_intro_video());


            Map<String, TsNtDictIndex> tsNtDictIndicesMap = (Map<String, TsNtDictIndex>)MemcachedUtils.get(MemcachedKey.tsNtDictIndicesMap);
            if (tsNtDictIndicesMap!=null && tdNtTeacherResult.getDict_id() != null){
                map.put("university" , tsNtDictIndicesMap.get(tdNtTeacherResult.getDict_id()).getCode_eng_name());
            } else {
                map.put("university" , null);
            }
            if (version >= 8) {
                Map<String, TsNtDictDeatil> tsNtDictDeatilsMap =
                        (Map<String, TsNtDictDeatil>) MemcachedUtils.get(MemcachedKey.tsNtDictDeatilsMap);
                String country = tdNtTeacherResult.getNationality() + "";
                if (tdNtTeacherResult.getNationality() == 0) {
                    country = "1";
                }
                map.put("nationality" ,
                        tsNtDictDeatilsMap.get(country).getCode_eng_name());
            }
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取个人资料成功");
            retInfo.setObj(map);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }
    }

    /**
     * @Purpose  首页
     * @version  3.0
     * @time     2015-01-09
     * @author   lizhun
     * @param    token
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/6/teatoken/teacher/index" ,method = {RequestMethod.GET,RequestMethod.POST},consumes = "application/json; charset=utf-8" )
    @ResponseBody
    public RetInfo index(@RequestHeader(value = "token", required = true) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            retInfo = teacherSearchService.findTeacherIndextwo(tdNtTeacherResult.getTea_id());

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @Purpose  获取国籍列表
     * @version  3.2
     * @time     2016-08-29
     * @author   lizhun
     * @param    token
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/teatoken/country/list" ,method = {RequestMethod.GET,RequestMethod.POST},consumes = "application/json; charset=utf-8" )
    @ResponseBody
    public RetInfo country(@RequestHeader(value = "token", required = true) String token) {
        RetInfo retInfo = new RetInfo();
        try {
            //打印入参
            logger.info(Constants.prefix_in);

            List<TsNtDictTea> tsNtDictIndices = ( List<TsNtDictTea>)MemcachedUtils.get(MemcachedKey.country);
            retInfo.setMark(ValidationMessages.getValue("MARKYES"));
            retInfo.setTip("获取数据成功");
            retInfo.setObj(tsNtDictIndices);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
    /**
     * @Purpose  修改国籍
     * @version  3.2
     * @time     2016-08-29
     * @author   lizhun
     * @param    token
     * @return   RetInfo
     */
    @RequestMapping(value = "/app/8/teatoken/country/update" ,method = {RequestMethod.GET,RequestMethod.POST},consumes = "application/json; charset=utf-8" )
    @ResponseBody
    public RetInfo update(@RequestBody Map<String,Integer> map
            , @RequestHeader(value = "token", required = false) String token) {
        try {
            //打印入参
            logger.info(Constants.prefix_in + JsonUtil.toJson(map));

            RetInfo retInfo;
            TdNtTeacherResult tdNtTeacherResult = (TdNtTeacherResult) MemcachedUtils.get(token);
            tdNtTeacherResult.setNationality(map.get("dict_id"));
            retInfo = teacherUpdateService.updateTeacher(tdNtTeacherResult);

            //打印出参
            logger.info(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        } catch (Exception e) {
            //记录错误日志
            RetInfo retInfo = RetInfo.getServerErrorInfo();
            logger.error(e.getMessage(), e);

            //打印出参
            logger.error(Constants.prefix_out + JsonUtil.toJson(retInfo));
            return retInfo;
        }

    }
}


